package adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.Calendar;
import java.util.List;

import gov.kqqzyyy.monkey.ydhl.R;
import objects.InPatient;

/**
 * Created by Monkey on 2017/3/6.
 * 病人列表适配器
 */

public class PatientsListAdapter extends BaseAdapter {

    private List<InPatient> list;
    private LayoutInflater inflater;

    public PatientsListAdapter(Context context) {
        this.inflater = LayoutInflater.from(context);
    }

    public void setList(List<InPatient> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (list != null && list.size() > 0) {
            return list.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (getCount() > 0 && list.get(position) != null) {
            return list.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_patient, parent, false);
            holder.patient_bed = convertView.findViewById(R.id.patient_bed);
            holder.patient_name = convertView.findViewById(R.id.patient_name);
            holder.patient_type = convertView.findViewById(R.id.patient_type);
            holder.patient_sex = convertView.findViewById(R.id.patient_sex);
            holder.patient_age = convertView.findViewById(R.id.patient_age);
            holder.patient_code = convertView.findViewById(R.id.patient_code);
            holder.patient_date = convertView.findViewById(R.id.patient_date);
            holder.patient_doctor = convertView.findViewById(R.id.patient_doctor);
            holder.patient_section = convertView.findViewById(R.id.patient_section);
            holder.patient_head = convertView.findViewById(R.id.patient_head);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (list != null && list.size() > 0) {
            if (list.get(position) != null) {
                InPatient inPatient = list.get(position);
                String brch = inPatient.getBRCH() == null ? "" : inPatient.getBRCH();
                holder.patient_bed.setText(brch);
                String brxm = inPatient.getBRXM() == null ? "" : inPatient.getBRXM();
                holder.patient_name.setText(brxm);
                String brxz = inPatient.getBRXZ() == null ? "" : inPatient.getBRXZ();
                holder.patient_type.setText(brxz);
                String brxb = inPatient.getBRXB() == null ? "" : inPatient.getBRXB();
                holder.patient_sex.setText(brxb);
                if (brxb.equals("男")) {
                    holder.patient_head.setImageResource(R.drawable.user_male);
                } else {
                    holder.patient_head.setImageResource(R.drawable.user_female);
                }
                String csny = inPatient.getCSNY() == null ? "" : inPatient.getCSNY();
                holder.patient_age.setText(getNL(csny));
                String zyhm = inPatient.getZYHM() == null ? "" : inPatient.getZYHM();
                holder.patient_code.setText(zyhm);
                String ryrq = inPatient.getRYRQ() == null ? "" : inPatient.getRYRQ().substring(0, 10);
                holder.patient_date.setText(ryrq);
                String ysxm = inPatient.getYSXM() == null ? "" : inPatient.getYSXM();
                holder.patient_doctor.setText(ysxm);
                String ksmc = inPatient.getKSMC() == null ? "" : inPatient.getKSMC();
                holder.patient_section.setText(ksmc);
            }
        }
        return convertView;
    }

    private String getNL(String csny) {
        Calendar calendar = Calendar.getInstance();
        int yearNow = calendar.get(Calendar.YEAR);
        int yearBorn = Integer.valueOf(csny.substring(0, 4));
        int deltaYear = yearNow - yearBorn + 1;
        return String.valueOf(deltaYear);
    }

    private class ViewHolder {
        private TextView patient_bed;
        private TextView patient_name;
        private TextView patient_type;
        private TextView patient_sex;
        private TextView patient_age;
        private TextView patient_code;
        private TextView patient_date;
        private TextView patient_doctor;
        private TextView patient_section;
        private SimpleDraweeView patient_head;
    }
}
